<?php


namespace app\api\controller\v9;


use app\api\controller\v2\Auth;
use app\common\model\Article as ArticleModel;
use think\Db;

class User extends Auth
{

    /**
     * 我的收藏
     * @return \think\response\Json
     * @throws \app\common\lib\exception\ApiException
     * @throws \think\Exception
     * @throws \think\exception\DbException
     */
    public function my_collect()
    {
        $data_is_from_cache = 1;//是否从缓存获取数据
        if ($data_is_from_cache)
        {
            //当前请求哪一页的数据
            $page = input('post.page/d', 1);

            //获取用户点赞文章的所有id
            $map = ['uid' => $this->user['id'], 'ucid' => $this->user['uc_id']];
            $cache = (new ArticleModel())->getUserArticleCollect($map, 1);
            $aids = $cache['ids'];
            $times = $cache['times'];

            $per_page = 10;//每页显示数量
            $aids = explode(',', $aids);//将$aids转换为数组
            $times = explode(',', $times);//将$times转换为数组
            array_shift($aids);//去除数组第一个元素
            array_shift($times);//去除数组第一个元素
            $aids = array_reverse($aids);//把aid倒序
            $times = array_reverse($times);//把文章的点赞时间倒序
            $total = count($aids);//统计有多少个id
            $data_list = getPaginatorData($total, $page, $per_page);//获取数据分页信息
            $aids = array_slice($aids, $data_list['start'], $per_page);//获取分页后的文章id数组
            $times = array_slice($times, $data_list['start'], $per_page);//获取分页后的文章id数组

            unset($data_list['start']);
            unset($data_list['end']);

            //根据id获取所有的文章数据
            foreach ($aids as $index => $aid)
            {
                $article = (new ArticleModel())->getArticleDetails($this->user['id'], ['id' => $aid, 'is_old' => true], 0);
                if ($article)
                {
                    unset($article['content']);
                    unset($article['author']);
                    unset($article['avatar']);
                    unset($article['thumb']);
                    unset($article['publish_time']);
                    unset($article['good']);
                    unset($article['collect']);
                    unset($article['read']);
                    unset($article['type_name']);
                    unset($article['type_name_en']);
                    unset($article['is_hot']);
                    unset($article['read_delay']);
                    $article['status'] = $article['status'] ? 1 : 2;
                    if (isset($times[$index]))
                    {
                        $article['create_time'] = $times[$index];
                    } else
                    {
                        $article['create_time'] = time();//赋值点赞时间
                    }
                    $data_list['data'][] = $article;
                }
            }

            return json(['code' => 200, 'data' => $data_list, 'msg' => 'Success.']);
        } else
        {//没加缓存之前的写法
            $data = Db::table('wechatadmin.system_article')->alias('a')
                ->field('a.id,a.title,a.introduction,ac.create_time,a.cid')
                ->join('wechatadmin.system_article_collect ac', 'a.id=ac.aid')
                ->where(['ac.ucid' => $this->user['uc_id'], 'a.status' => 1])
                ->order('ac.create_time DESC')
                ->paginate(10, false, ['query' => request()->param()]);

            return json(['code' => 200, 'data' => $data, 'msg' => 'Success.']);
        }

    }

}
